//
//  xmlparser.m
//  PhyloHunter
//
//  Created by Chris Hamilton on 7/27/13.
//  Copyright (c) 2013 The Gigglers. All rights reserved.
//

#import "xmlparser.h"

@implementation xmlparser

@synthesize currentquestion = _currentquestion;

NSMutableString *nodecontent;
NSXMLParser *parser;

- (id) loadXMLByURL:(NSString *)serverURL
{
    //load url into parser to begin parsing
    _currentquestion = [[question alloc] init];
    NSURL *url = [NSURL URLWithString:serverURL];
    NSData *data = [[NSData alloc] initWithContentsOfURL:url];
    parser = [[NSXMLParser alloc] initWithData:data];
    parser.delegate = self;
    [parser parse];
    return self;
}

//trims the white space from text
- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    nodecontent = (NSMutableString *) [string stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
}


- (void) parser:(NSXMLParser *)parser didstartElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)
qName attrubutes:(NSDictionary *)attributeDict
{
    
}

- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)
qName
{
    //this method is called for each element of the xml.  Since the XML is basic, we simply compare the
    //elementname to the name we are looking for and store the value in the currentquestion for the
    //appropriete variable
    if ([elementname isEqualToString:@"text"])
    {
        self.currentquestion.text = nodecontent;
    }
    
    if ([elementname isEqualToString:@"answer1"])
    {
        self.currentquestion.answer1 = nodecontent;
    }
    
    if ([elementname isEqualToString:@"answer2"])
    {
        self.currentquestion.answer2 = nodecontent;
    }
    
    if ([elementname isEqualToString:@"answer3"])
    {
        self.currentquestion.answer3 = nodecontent;
    }
    
    if ([elementname isEqualToString:@"answer4"])
    {
        self.currentquestion.answer4 = nodecontent;
    }
    
    if ([elementname isEqualToString:@"correct"])
    {
        self.currentquestion.correct = nodecontent;
    }
}

@end
